

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fantuan">
  <meta name="keywords" content="">
  
    <meta name="description" content="Jest Note配置单元测试与集成测试 单元测试：是指对软件中的最小可测试单元进行检查和验证。前端所说的单元测试就是对一个模块进行测试。也就是说前端测试的时候，你测试的东西一定是一个模块   集成测试：也叫组装测试或者联合测试。在单元测试的基础上，将所有模块按照涉及要求组装成为子系统或系统，进行集成测试。  覆盖率报告 coverageDirecotry：指定覆盖率报告生成文件夹，指定文件夹的同">
<meta property="og:type" content="article">
<meta property="og:title" content="使用Jest进行前端测试">
<meta property="og:url" content="https://gitee.com/swallow_week/blog/2022/06/14/%E4%BD%BF%E7%94%A8Jest%E8%BF%9B%E8%A1%8C%E5%89%8D%E7%AB%AF%E6%B5%8B%E8%AF%95/index.html">
<meta property="og:site_name" content="饭团君的博客">
<meta property="og:description" content="Jest Note配置单元测试与集成测试 单元测试：是指对软件中的最小可测试单元进行检查和验证。前端所说的单元测试就是对一个模块进行测试。也就是说前端测试的时候，你测试的东西一定是一个模块   集成测试：也叫组装测试或者联合测试。在单元测试的基础上，将所有模块按照涉及要求组装成为子系统或系统，进行集成测试。  覆盖率报告 coverageDirecotry：指定覆盖率报告生成文件夹，指定文件夹的同">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-06-14T09:51:50.000Z">
<meta property="article:modified_time" content="2022-06-15T02:36:37.607Z">
<meta property="article:author" content="Fantuan">
<meta property="article:tag" content="测试">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>使用Jest进行前端测试 - 饭团君的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"gitee.com","root":"/blog/","version":"1.9.1","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>

  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="使用Jest进行前端测试"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-06-14 01:51" pubdate>
          2022年6月14日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          59 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">使用Jest进行前端测试</h1>
            
            <div class="markdown-body">
              
              <h3 id="Jest-Note"><a href="#Jest-Note" class="headerlink" title="Jest Note"></a>Jest Note</h3><h4 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h4><h5 id="单元测试与集成测试"><a href="#单元测试与集成测试" class="headerlink" title="单元测试与集成测试"></a>单元测试与集成测试</h5><blockquote>
<p>单元测试：是指对软件中的最小可测试单元进行检查和验证。前端所说的单元测试就是对一个模块进行测试。也就是说前端测试的时候，你测试的东西一定是一个模块</p>
</blockquote>
<blockquote>
<p>集成测试：也叫组装测试或者联合测试。在单元测试的基础上，将所有模块按照涉及要求组装成为子系统或系统，进行集成测试。</p>
</blockquote>
<h5 id="覆盖率报告"><a href="#覆盖率报告" class="headerlink" title="覆盖率报告"></a>覆盖率报告</h5><blockquote>
<p>coverageDirecotry：指定覆盖率报告生成文件夹，指定文件夹的同时，表示启用生成覆盖率报告功能</p>
</blockquote>
<h5 id="开启自动测试"><a href="#开启自动测试" class="headerlink" title="开启自动测试"></a>开启自动测试</h5><p><code>package.json</code>配置如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs javascript">&#123;<br>  <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;jesttest&quot;</span>,<br>  <span class="hljs-string">&quot;version&quot;</span>: <span class="hljs-string">&quot;1.0.0&quot;</span>,<br>  <span class="hljs-string">&quot;description&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>  <span class="hljs-string">&quot;main&quot;</span>: <span class="hljs-string">&quot;index.js&quot;</span>,<br>  <span class="hljs-string">&quot;scripts&quot;</span>: &#123;<br>    <span class="hljs-string">&quot;test&quot;</span>: <span class="hljs-string">&quot;jest --watchAll&quot;</span>,<br>  &#125;,<br>  <span class="hljs-string">&quot;author&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,<br>  <span class="hljs-string">&quot;license&quot;</span>: <span class="hljs-string">&quot;ISC&quot;</span>,<br>  <span class="hljs-string">&quot;devDependencies&quot;</span>: &#123;<br>    <span class="hljs-string">&quot;jest&quot;</span>: <span class="hljs-string">&quot;^24.8.0&quot;</span><br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h5 id="匹配器"><a href="#匹配器" class="headerlink" title="匹配器"></a>匹配器</h5><ul>
<li><p>toBe()匹配器</p>
<blockquote>
<p>等同于<code>===</code>的，也就是我们常说的严格相等。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;测试严格相等&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> a = <span class="hljs-number">1</span>   <br>    <span class="hljs-title function_">expect</span>(a).<span class="hljs-title function_">toBe</span>(<span class="hljs-number">1</span>)<br>&#125;) <br></code></pre></td></tr></table></figure>


</li>
<li><p>toEqual()匹配器</p>
<blockquote>
<p>只要内容相等，就可以通过测试</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;测试严格相等&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> a = &#123;<span class="hljs-attr">number</span>:<span class="hljs-string">&#x27;007&#x27;</span>&#125;   <br>    <span class="hljs-title function_">expect</span>(a).<span class="hljs-title function_">toEqual</span>(&#123;<span class="hljs-attr">number</span>:<span class="hljs-string">&#x27;007&#x27;</span>&#125;)<br>&#125;) <br></code></pre></td></tr></table></figure>

<blockquote>
<p><strong>但是如果我们把<code>a</code>的值改为<code>undefined</code>，测试用例就通过不了啦</strong>。</p>
</blockquote>
</li>
<li><p>toBeNul()匹配器</p>
<blockquote>
<p>只匹配<code>null</code>值，需要注意的是不匹配<code>undefined</code>的值</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeNull测试&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> a = <span class="hljs-literal">null</span>   <br>    <span class="hljs-title function_">expect</span>(a).<span class="hljs-title function_">toBeNull</span>()<br>&#125;) <br></code></pre></td></tr></table></figure>


</li>
<li><p>toBeUndifined()匹配器</p>
<blockquote>
<p>只匹配undefined值</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeUndefined测试&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> a = <span class="hljs-literal">undefined</span>   <br>    <span class="hljs-title function_">expect</span>(a).<span class="hljs-title function_">toBeUndefined</span>()<br>&#125;) <br></code></pre></td></tr></table></figure>

<blockquote>
<p>这里需要注意的是，我给一个<code>null</code>值也是可以通过测试的。</p>
</blockquote>
</li>
<li><p>toBeDefined()匹配器</p>
<blockquote>
<p>是只要定义过了，都可以匹配成功</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeDefined测试&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> a = <span class="hljs-string">&#x27;jspang&#x27;</span>  <br>    <span class="hljs-title function_">expect</span>(a).<span class="hljs-title function_">toBeDefined</span>()<br>&#125;) <br></code></pre></td></tr></table></figure>


</li>
<li><p>toBeTruthy()匹配器</p>
<blockquote>
<p>这个是<code>true</code>匹配器，判断这个变量是不是true，不是Boolean值会报错</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeTruthy 测试&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> a = <span class="hljs-literal">true</span><br>    <span class="hljs-title function_">expect</span>(a).<span class="hljs-title function_">toBeTruthy</span>()<br>&#125;) <br></code></pre></td></tr></table></figure>

<blockquote>
<p>0 和 1 也会报错</p>
</blockquote>
</li>
<li><p>toBeFalsy()匹配器</p>
<blockquote>
<p>这个是<code>false</code>匹配器，判断这个变量是不是false，不是Boolean值会报错</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeTruthy 测试&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> a = <span class="hljs-literal">false</span><br>    <span class="hljs-title function_">expect</span>(a).<span class="hljs-title function_">toBeFalsy</span>()<br>&#125;) <br></code></pre></td></tr></table></figure>
</li>
<li><p>toBeGreaterThan()匹配器</p>
<blockquote>
<p>相当于  大于</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeGreaterThan匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> count = <span class="hljs-number">10</span><br>    <span class="hljs-title function_">expect</span>(count).<span class="hljs-title function_">toBeGreaterThan</span>(<span class="hljs-number">9</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>


</li>
<li><p>toBeLessThan()匹配器</p>
<blockquote>
<p>相当于  小于</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeGreaterThan匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> count = <span class="hljs-number">10</span><br>    <span class="hljs-title function_">expect</span>(count).<span class="hljs-title function_">toBeGreaterThan</span>(<span class="hljs-number">11</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>


</li>
<li><p>toBeGreaterThanOrEqual()匹配器</p>
<blockquote>
<p>相当于 大于等于</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeGreaterThan匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> count = <span class="hljs-number">10</span><br>    <span class="hljs-title function_">expect</span>(count).<span class="hljs-title function_">toBeGreaterThan</span>(<span class="hljs-number">10</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>


</li>
<li><p>toBeLessThanOrEqual()匹配器</p>
<blockquote>
<p>相当于 小于等于</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeGreaterThan匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> count = <span class="hljs-number">10</span><br>    <span class="hljs-title function_">expect</span>(count).<span class="hljs-title function_">toBeGreaterThan</span>(<span class="hljs-number">10</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>


</li>
<li><p>toBeCloseTo()匹配器</p>
<blockquote>
<p>可以自动消除<code>JavaScript</code>浮点精度错误的匹配器 </p>
<p>例如 0.1+0.2&#x3D;0.300000000004  用toEqual()测试不会通过 , toBeCloseTo则可以</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toBeCloseTo匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> one = <span class="hljs-number">0.1</span><br>    <span class="hljs-keyword">const</span> two = <span class="hljs-number">0.2</span><br>    <span class="hljs-title function_">expect</span>(one + two).<span class="hljs-title function_">toBeCloseTo</span>(<span class="hljs-number">0.3</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>


</li>
<li><p>toMatch()匹配器</p>
<blockquote>
<p>判断是否存在子集 适用于字符串</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toMatch匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> str=<span class="hljs-string">&quot;谢大脚、刘英、小红&quot;</span><br>    <span class="hljs-title function_">expect</span>(str).<span class="hljs-title function_">toMatch</span>(<span class="hljs-string">&#x27;谢大脚&#x27;</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>支持正则表达式</p>
</blockquote>
</li>
<li><p>toContain()匹配器</p>
<blockquote>
<p>判断是否存在子集 适用于数组</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toContain匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-keyword">const</span> arr=[<span class="hljs-string">&#x27;谢大脚&#x27;</span>,<span class="hljs-string">&#x27;刘英&#x27;</span>,<span class="hljs-string">&#x27;小红&#x27;</span>]<br>    <span class="hljs-title function_">expect</span>(arr).<span class="hljs-title function_">toContain</span>(<span class="hljs-string">&#x27;谢大脚&#x27;</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>也支持set</p>
</blockquote>
</li>
<li><p>toThrow()匹配器</p>
<blockquote>
<p>专门对异常进行处理的匹配器，可以检测一个方法会不会抛出异常</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> <span class="hljs-title function_">throwNewErrorFunc</span> = (<span class="hljs-params"></span>)=&gt;&#123;<br>    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">&#x27;this is a new error&#x27;</span>)<br>&#125;<br><br><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toThrow匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-title function_">expect</span>(throwNewErrorFunc).<span class="hljs-title function_">toThrow</span>()<br>&#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>也可以对这个匹配器中加一些字符串，意思就是抛出的异常必须和字符串相对应</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> <span class="hljs-title function_">throwNewErrorFunc</span> = (<span class="hljs-params"></span>)=&gt;&#123;<br>    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">&#x27;this is a new error&#x27;</span>)<br>&#125;<br><br><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toThrow匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-title function_">expect</span>(throwNewErrorFunc).<span class="hljs-title function_">toThrow</span>(<span class="hljs-string">&#x27;this is a new error&#x27;</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>


</li>
<li><p>not匹配器</p>
<blockquote>
<p>not<code>匹配器是</code>Jest<code>中比较特殊的匹配器，意思就是</code>相反<code>或者说</code>取反</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> <span class="hljs-title function_">throwNewErrorFunc</span> = (<span class="hljs-params"></span>)=&gt;&#123;<br>    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">&#x27;this is a new error&#x27;</span>)<br>&#125;<br><br><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;toThrow匹配器&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-title function_">expect</span>(throwNewErrorFunc).<span class="hljs-property">not</span>.<span class="hljs-title function_">toThrow</span>()<br>&#125;)<br></code></pre></td></tr></table></figure></li>
</ul>
<h5 id="让Jest支持import和ES6语法"><a href="#让Jest支持import和ES6语法" class="headerlink" title="让Jest支持import和ES6语法"></a>让Jest支持import和ES6语法</h5><blockquote>
<p>Jest默认支持的是<code>CommonJS</code>规范，也就是<code>Node.js</code>中的语法，他只支持<code>require</code>这种引用</p>
</blockquote>
<ul>
<li><p>安装Babel转换器</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">cnpm install @babel/core@7.4.5 @babel/preset-env@7.4.5 -D<br></code></pre></td></tr></table></figure>
</li>
<li><p>配置Babel<br>根目录下新建一个<code>.babelrc</code>的文件,添加如下代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs javascript">&#123;<br>    <span class="hljs-string">&quot;presets&quot;</span>:[<br>        [<br>                <span class="hljs-string">&quot;@babel/preset-env&quot;</span>,&#123;<br>                <span class="hljs-string">&quot;targets&quot;</span>:&#123;<br>                    <span class="hljs-string">&quot;node&quot;</span>:<span class="hljs-string">&quot;current&quot;</span><br>                &#125;<br>            &#125;<br>        ]<br>    ]<br>&#125;<br></code></pre></td></tr></table></figure></li>
</ul>
<blockquote>
<p>其实在<code>Jest</code>里有一个<code>babel-jest</code>组件，我们在使用<code>yarn test</code>的时候，它先去检测开发环境中是否安装了<code>babel</code>，也就是查看有没有<code>babel-core</code>，如果有<code>bable-core</code>就会去查看<code>.babelrc</code>配置文件，根据配置文件进行转换，转换完成，再进行测试</p>
</blockquote>
<h5 id="异步代码的测试方法"><a href="#异步代码的测试方法" class="headerlink" title="异步代码的测试方法"></a>异步代码的测试方法</h5><ol>
<li><p>回调函数式<br>安装axios</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript">cnpm i -S axios<br></code></pre></td></tr></table></figure>

<p>编写请求接口函数 fetchData.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;axios&#x27;</span><br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> <span class="hljs-title function_">fetchData</span> = (<span class="hljs-params">fn</span>)=&gt;&#123;<br>    axios.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;http://a.jspang.com/jestTest.json&#x27;</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">response</span>)=&gt;</span>&#123;<br>        <span class="hljs-title function_">fn</span>(response.<span class="hljs-property">data</span>)<br>    &#125;)<br>&#125;<br></code></pre></td></tr></table></figure>

<p>编写测试方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> &#123; fetchData &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./fetchData.js&#x27;</span><br><br><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;fetchData 测试&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>   <span class="hljs-title function_">fetchData</span>(<span class="hljs-function">(<span class="hljs-params">data</span>)=&gt;</span>&#123;<br>       <span class="hljs-title function_">expect</span>(data).<span class="hljs-title function_">toEqual</span>(&#123;<br>           <span class="hljs-attr">success</span>: <span class="hljs-literal">true</span><br>       &#125;)<br>		<span class="hljs-title function_">done</span>()<br>   &#125;)<br>  &#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>加入一个done方法，保证我们的回调已经完成了，这时候我们表示测试完成</p>
</blockquote>
</li>
<li><p>直接返回Promise<br>请求接口还是用上面代码<br>测试代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> &#123; fetchData , fetchTwoData &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./fetchData.js&#x27;</span><br><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;FetchTwoData 测试&#x27;</span>, <span class="hljs-function">()=&gt;</span>&#123;<br>       <span class="hljs-keyword">return</span>  <span class="hljs-title function_">fetchTwoData</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">response</span>)=&gt;</span>&#123;<br>            <span class="hljs-title function_">expect</span>(response.<span class="hljs-property">data</span>).<span class="hljs-title function_">toEqual</span>(&#123;<br>                <span class="hljs-attr">success</span>: <span class="hljs-literal">true</span><br>            &#125;)<br>        &#125;)<br>  &#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>要注意的是要使用<code>return</code>才能测试成功</p>
</blockquote>
</li>
<li><p>不存在的接口测试方法<br>有些时候会测试请求接口时会出现404，403，401，500等情况<br>新增错误接口方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> <span class="hljs-title function_">fetchThreeData</span> = (<span class="hljs-params"></span>)=&gt;&#123;<br>    <span class="hljs-keyword">return</span> axios.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;http://a.jspang.com/jestTest_error.json&#x27;</span>)<br>&#125;<br></code></pre></td></tr></table></figure>

<p>编写测试方法:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;FetchThreeData 测试&#x27;</span>, <span class="hljs-function">()=&gt;</span>&#123;<br>      expect.<span class="hljs-title function_">assertions</span>(<span class="hljs-number">1</span>)  <span class="hljs-comment">// 断言，必须执行一次expect</span><br>      <span class="hljs-keyword">return</span> <span class="hljs-title function_">fetchThreeData</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">e</span>)=&gt;</span>&#123;<br>        <span class="hljs-title function_">expect</span>(e.<span class="hljs-title function_">toString</span>().<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">&#x27;404&#x27;</span>)&gt; -<span class="hljs-number">1</span>).<span class="hljs-title function_">toBe</span>(<span class="hljs-literal">true</span>)<br>      &#125;)<br>  &#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>注意：这里添加<code>expect.assertions</code>,表示这个测试方法必须执行一次expect否则判定为失败。<br>为什么要加这一段代码呢？因为上述的测试方法只有接口出错时才会执行，若接口没有出错就不会执行catch方法，Jest默认该测试方法通过了</p>
</blockquote>
</li>
<li><p>async…await<br>新增接口方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript"> <span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> <span class="hljs-title function_">fetchFourData</span> = (<span class="hljs-params"></span>)=&gt;&#123;<br>    <span class="hljs-keyword">return</span> axios.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;http://a.jspang.com/jestTest.json&#x27;</span>)<br>&#125;<br></code></pre></td></tr></table></figure>

<p>编写测试方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;FetchFourData 测试&#x27;</span>, <span class="hljs-title function_">async</span>()=&gt;&#123;<br>        <span class="hljs-comment">//resolves把现有对象转换成Promise对象，</span><br>        <span class="hljs-comment">//toMatchObject 匹配对象中的属性</span><br>        <span class="hljs-keyword">await</span> <span class="hljs-title function_">expect</span>(<span class="hljs-title function_">fetchFourData</span>()).<span class="hljs-property">resolves</span>.<span class="hljs-title function_">toMatchObject</span>(&#123;<br>            <span class="hljs-attr">data</span>:&#123;<br>                <span class="hljs-attr">success</span>:<span class="hljs-literal">true</span><br>            &#125;<br>        &#125;)<br>&#125;)<br></code></pre></td></tr></table></figure>

<p>更简单的测试方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">test</span>(<span class="hljs-string">&#x27;FetchFourData 测试&#x27;</span>, <span class="hljs-title function_">async</span>()=&gt;&#123;<br>        <span class="hljs-keyword">const</span> response  = <span class="hljs-keyword">await</span> <span class="hljs-title function_">fetchFourData</span>()<br>        <span class="hljs-title function_">expect</span>(response.<span class="hljs-property">data</span>).<span class="hljs-title function_">toEqual</span>(&#123;<br>            success : <span class="hljs-literal">true</span><br>        &#125;)<br>&#125;)<br></code></pre></td></tr></table></figure></li>
</ol>
<h5 id="Jest的四个钩子函数"><a href="#Jest的四个钩子函数" class="headerlink" title="Jest的四个钩子函数"></a>Jest的四个钩子函数</h5><ol>
<li><p>beforeAll: 在所有测试用例之前进行执行</p>
</li>
<li><p>afterAll: 在完成所有测试用例之后才执行的函数</p>
</li>
<li><p>beforeEach: 在每个测试用例前都会执行一次的钩子函数</p>
</li>
<li><p>afterEach: 是在每次测试用例完成测试之后执行一次的钩子函数</p>
<blockquote>
<p>生命周期执行顺序：beforeAll -&gt; beforeEach -&gt; 开始执行测试代码 -&gt; afterEach -&gt; afterAll</p>
</blockquote>
</li>
</ol>
<h5 id="Jest中对测试用例分组"><a href="#Jest中对测试用例分组" class="headerlink" title="Jest中对测试用例分组"></a>Jest中对测试用例分组</h5><p>将测试方法进行分组测试，需要使用到describe()</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">describe</span>(<span class="hljs-string">&#x27;A&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-title function_">test</span>(xxx)<br>    <span class="hljs-title function_">test</span>(xxx)<br>&#125;)<br><span class="hljs-title function_">describe</span>(<span class="hljs-string">&#x27;B&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-title function_">test</span>(xxx)<br>    <span class="hljs-title function_">test</span>(xxx)<br>&#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>分组的好处实际就就是要让测试用例开起来更有层次感</p>
</blockquote>
<h5 id="钩子函数的作用域"><a href="#钩子函数的作用域" class="headerlink" title="钩子函数的作用域"></a>钩子函数的作用域</h5><ul>
<li><p>特点</p>
<ul>
<li>钩子函数在父级分组可作用域子集，类似继承</li>
<li>钩子函数同级分组作用域互不干扰，各起作用</li>
<li>先执行外部的钩子函数，再执行内部的钩子函数</li>
</ul>
</li>
<li><p>例子</p>
<blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> xx <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;xx&#x27;</span><br><span class="hljs-title function_">describe</span>(<span class="hljs-string">&#x27;最外层A&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-title function_">beforeAll</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;A&#x27;</span>))&#125;)<br>    <span class="hljs-title function_">beforeEatch</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;A&#x27;</span>)&#125;)<br>    <span class="hljs-title function_">describe</span>(<span class="hljs-string">&#x27;里层B&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>        <span class="hljs-title function_">beforeAll</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;B&#x27;</span>))&#125;)<br>    	<span class="hljs-title function_">beforeEatch</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;B&#x27;</span>)&#125;)<br>        <span class="hljs-title function_">test</span>(xxx)<br>        <span class="hljs-title function_">afterEach</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;B&#x27;</span>)&#125;)<br>        <span class="hljs-title function_">afterAll</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;B&#x27;</span>)&#125;)<br>    &#125;)<br>    <span class="hljs-title function_">describe</span>(<span class="hljs-string">&#x27;里层C&#x27;</span>,<span class="hljs-function">()=&gt;</span>&#123;<br>        <span class="hljs-title function_">beforeAll</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;C&#x27;</span>))&#125;)<br>    	<span class="hljs-title function_">beforeEatch</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;C&#x27;</span>)&#125;)<br>        <span class="hljs-title function_">test</span>(xxx)<br>        <span class="hljs-title function_">afterEach</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;C&#x27;</span>)&#125;)<br>        <span class="hljs-title function_">afterAll</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;C&#x27;</span>)&#125;)<br>    &#125;)<br>    <span class="hljs-title function_">afterEach</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;A&#x27;</span>)&#125;)<br>    <span class="hljs-title function_">afterAll</span>(<span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;A&#x27;</span>)&#125;)<br>&#125;)<br></code></pre></td></tr></table></figure>

<p>以上代码的执行结果 AA BBBB CCCC AA</p>
</blockquote>
</li>
</ul>
<p><code>only</code>表示只执行一个测试用例</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs javascript">test.<span class="hljs-title function_">only</span>(xxx)<span class="hljs-comment">// 执行</span><br><span class="hljs-title function_">test</span>(xxx)<span class="hljs-comment">// 跳过</span><br></code></pre></td></tr></table></figure>
              
            </div>
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/Study-Notes/" class="category-chain-item">Study Notes</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/%E6%B5%8B%E8%AF%95/">#测试</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>使用Jest进行前端测试</div>
      <div>https://gitee.com/swallow_week/blog/2022/06/14/使用Jest进行前端测试/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Fantuan</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年6月14日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2022/06/15/VideoJS%E5%A2%9E%E5%8A%A0%E6%B8%85%E6%99%B0%E5%BA%A6%E6%8E%A7%E4%BB%B6/" title="VideoJS增加清晰度控件">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">VideoJS增加清晰度控件</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  






    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>






  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script>
  (function() {
    var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
    var enableCopy = CONFIG.copy_btn;
    if (!enableLang && !enableCopy) {
      return;
    }

    function getBgClass(ele) {
      return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark';
    }

    var copyTmpl = '';
    copyTmpl += '<div class="code-widget">';
    copyTmpl += 'LANG';
    copyTmpl += '</div>';
    jQuery('.markdown-body pre').each(function() {
      var $pre = jQuery(this);
      if ($pre.find('code.mermaid').length > 0) {
        return;
      }
      if ($pre.find('span.line').length > 0) {
        return;
      }

      var lang = '';

      if (enableLang) {
        lang = CONFIG.code_language.default;
        if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
          lang = $pre[0].children[0].classList[1];
        } else if ($pre[0].getAttribute('data-language')) {
          lang = $pre[0].getAttribute('data-language');
        } else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
          lang = $pre[0].children[0].classList[1];
          $pre.parent().addClass('code-wrapper');
        } else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
          $pre.wrap('<div class="code-wrapper"></div>');
        }
        lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default);
      }
      $pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
        getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));

      if (enableCopy) {
        Fluid.utils.createScript('https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js', function() {
          var clipboard = new window.ClipboardJS('.copy-btn', {
            target: function(trigger) {
              var nodes = trigger.parentNode.childNodes;
              for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].tagName === 'CODE') {
                  return nodes[i];
                }
              }
            }
          });
          clipboard.on('success', function(e) {
            e.clearSelection();
            e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
            setTimeout(function() {
              e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
            }, 2000);
          });
        });
      }
    });
  })();
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
